package com.funtl.my.shop.web.admin.abstracts;

import com.funtl.my.shop.domain.TbContent;
import com.futl.my.shop.commons.dto.PageInfo;
import com.futl.my.shop.commons.persistence.BaseDao;
import com.futl.my.shop.commons.persistence.BaseEntity;
import com.futl.my.shop.commons.persistence.BaseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author tw
 * @version 1.0
 * @date 2020/6/2 19:45T
 */
public abstract class AbstractBaseServiceImpl<T extends BaseEntity, D extends BaseDao<T>> implements BaseService<T> {

    @Autowired
    protected D dao;

    /**
     * 查询全部数据
     * @return
     */
    @Override
    public List<T> selectAll(){
        return dao.selectAll();
    };

    /**
     * 删除
     * @param id
     */
    @Override
    @Transactional(readOnly = false)
    public void delete(long id){
        dao.delete(id);
    }

    /**
     * 根据id查询
     * @param id
     * @return
     */
    @Override
    public T getById(long id){
        return dao.getById(id);
    }

    /**
     * 更新
     * @param entity
     */
    @Override
    @Transactional(readOnly = false)
    public void update(T entity){
        dao.update(entity);
    }


    /**
     * 批量删除
     * @param idArray
     */
    @Override
    @Transactional(readOnly = false)
    public void deleteMulti(String[] idArray){
        dao.deleteMulti(idArray);
    }

    /**
     * 查询总数
     * @param entity
     * @return
     */
    @Override
    public int count(T entity){
        return dao.count(entity);
    }

    /**
     * 新增
     * @param entity
     */
    @Transactional(readOnly = false)
    public void insert(T entity) {
        dao.save(entity);
    }


    /**
     * 分页查询
     * @param start 起始位置
     * @param length 长度
     * @return
     */
    @Override
    public PageInfo<T> page(int start, int length, int draw, T entity) {
        PageInfo<T> pageInfo = new PageInfo<>();
        int total = count(entity);
        pageInfo.setDraw(draw);
        pageInfo.setRecordsTotal(total);
        pageInfo.setRecordsFiltered(total);
        Map<String, Object> params = new HashMap<>();
        params.put("start", start);
        params.put("length", length);
        params.put("pageParams", entity);
        pageInfo.setData(dao.page(params));
        return pageInfo;
    }
}
